<template>
	<view :class="isFocus?'content-box focus':'content-box'"  >
	
		<view class="input-area" >
			<input ref="inputRef" v-model="inputValue" type="text" :clearable="false" :focus="isFocus" hold-keyboard
			@confirm="send"	placeholder="发表评论" @focus="inputFocus" />
			<!-- <input type="text" /> -->
		</view>
		
		<view class="praise-area">
			<view v-if="!isFocus" class="item1">
				<text>{{praiseTotal}}</text>
				<image src="../../static/images/squareImg/none_praise.png"></image>
			</view>
			<view v-show="isFocus">
				<u-button type="primary" size="medium" @click="send" >发表</u-button>
			</view>
		</view>
		<!-- toast提示框 -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		name: "commentInput",
		props:{
			praiseTotal:Number
		},
		data() {
			return {
				inputValue: '',
				// 输入框是否聚焦
				isFocus: false,
				
			};
		},
		created() {
			uni.onKeyboardHeightChange(res => {
				//监听软键盘的高度 
				//当点击软键盘自带的收起按钮的时候也就是会隐藏软键盘 这时候监听到的软键盘的高度就是0 、
				//让输入框取消焦点 这时候再去输入内容的时候 输入框就会弹起
				if (res.height == 0) {
					this.isFocus = false;

				}

			})
		},
		methods: {
			// input输入框聚焦
			inputFocus() {
				this.isFocus = true
			},
			// input输入框失去焦点
			inputBlur() {
				this.isFocus = false
			},
			// 发布信息
			send() {
				if (this.inputValue.trim() == '') {
					this.inputValue = ''
					return this.$refs.uToast.show({
						title: '内容不能为空',
						type: 'error'
					})
				}
				uni.request({
					url:'http://1.116.242.41:4000/square/sendComment',
					data:{
						commentContent:this.inputValue,
						userId:1,
						squareId:uni.getStorageSync("squareId")
					},
					method:"POST",
					success: (res) => {
						this.$refs.uToast.show({
							title: '发表成功',
							type: 'success'
						})
						this.isFocus = false
						this.inputValue = ''
						uni.$emit("dynamic","updateComment")
					},
					fail: () => {
						console.log("发送留言失败")
					}
				})
				
			}
		},
		mounted() {
			
			uni.$on("commentInput", msg => {
				if (msg == 'inputBlur') {
					this.inputBlur()
				}
			})
		}
	}
</script>

<style lang="scss">
	.focus {

		.input-area {

			input {
				width: 520rpx !important;
				height: 70rpx !important;
				background-color: #ECEEF6 !important;
				padding: 0 20rpx !important;
				border-radius: 0rpx !important;
				margin-bottom: 20rpx !important;
			}
		}

		button {
			width: 10rpx !important;
			margin-bottom: 20rpx !important;
		}

	}

	.content-box {
		width: 750rpx;
		height: 110rpx;
		background-color: white;
		border-top: solid 1px #F3F3F3;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 10rpx;
		position: fixed;
		bottom: 0;

		.input-area {

			input {
				width: 580rpx;
				height: 70rpx;
				border-radius: 50rpx;
				background-color: #ECEEF6;
				padding: 0 20rpx;
			}
		}


		.praise-area {
			display: flex;
			align-items: center;

			.item1 {
				display: flex;
				align-items: center;
			}

			image {
				width: 50rpx;
				height: 50rpx;

			}
		}
	}
</style>
